package cn.ticket.controller;

import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.ticket.model.Routes;
import cn.ticket.model.Workbuses;
import cn.ticket.service.RoutesService;
import cn.ticket.service.Utility;
import cn.ticket.service.WorkBusesService;

@Controller
@RequestMapping("workBuses")
public class WorkBusesController {
	@Autowired
	WorkBusesService workBusesService;
	@Autowired 
	RoutesService routeService;
	
	@RequestMapping(value="insert",method=RequestMethod.POST,produces={"application/json;charset=utf-8"})
	@ResponseBody
	public String insert(@RequestBody Workbuses record){
		Routes route=routeService.selectByPrimaryKey(record.getRouteid());
		if (route.getOrientation().equals("成都")) {
			record.setOrientation((byte) 0);
		}
		else {
			record.setOrientation((byte) 1);
		}
		int num=workBusesService.insertSelective(record);
		Map<String, Object> result=new HashMap<>();
		if (num==1) {
			result.put("status", 1001);
		}
		else {
			result.put("status", 1002);
		}
		return Utility.gson.toJson(result);
	}
	@RequestMapping(value="insertDayBuses",method=RequestMethod.POST,produces={"application/json;charset=utf-8"})
	@ResponseBody
	public String insertDayBuses(@RequestBody Map<String, Object> args) throws ParseException{
		String day=(String) args.get("day");
		Map<String, Object> result=new HashMap<>();
		if (workBusesService.generateWorkBuses(day)) {
			result.put("status", 1001);
		}
		else {
			result.put("status", 1002);
		}
		return Utility.gson.toJson(result);
	}
	@RequestMapping(value="update",method=RequestMethod.POST,produces={"application/json;charset=utf-8"})
	@ResponseBody
	public String update(@RequestBody Workbuses record){
		int num=workBusesService.updateByPrimaryKeySelective(record);
		Map<String, Object> result=new HashMap<>();
		if (num==1) {
			result.put("status", 1001);
		}
		else {
			result.put("status", 1002);
		}
		return Utility.gson.toJson(result);
	}
	
	@RequestMapping(value="delete",method=RequestMethod.POST,produces={"application/json;charset=utf-8"})
	@ResponseBody
	public String delete(@RequestBody Map<String, String> args){
		Integer id=Integer.parseInt(args.get("id"));
		int num = workBusesService.deleteByPrimaryKey(id);
		Map<String, Object> result=new HashMap<>();
		if (num==1) {
			result.put("status", 1001);
		}
		else {
			result.put("status", 1002);
		}
		return Utility.gson.toJson(result);
	}
	
	@RequestMapping(value="getByCount",produces={"application/json;charset=utf-8"})
	@ResponseBody
	public String getByCount(@RequestBody Map<String, String> args){
		Map<String, Object> result=new HashMap<>();
		Integer start=Integer.parseInt(args.get("start"));
		Integer count=Integer.parseInt(args.get("count"));
		List<Map<String, Object>> workbuses=workBusesService.getByCount(start, count);
		if (workbuses.isEmpty()) {
			result.put("status", 1002);
		}
		else {
			result.put("status", 1001);
			result.put("body", workbuses);
		}
		return Utility.gson.toJson(result);
	}
	@RequestMapping(value="getByOrientation",method=RequestMethod.GET,produces={"application/json;charset=utf-8"})
	@ResponseBody
	public String getByOrientation(@RequestBody Map<String, String> args){
		
		return null;
	}
	@RequestMapping(value="getValidate0",method=RequestMethod.GET,produces={"application/json;charset=utf-8"})
	@ResponseBody
	public String getValidate0(){
		
	//	List<Workbuses> workbuses=workBusesService.getValidate0();
		Date startDate=new Date(new Date().getTime()-1000*60*60*18);
		Date endDate=new Date(new Date().getTime()+1000*60*60*24);
		List<Map<String, Object>> workbuses=workBusesService.getInfoByDateAndOrientation("成都", startDate, endDate);
		Map<String, Object> result=new HashMap<>();
		if (workbuses.isEmpty()) {
			result.put("status", 1002);
		}
		else {
			result.put("status", 1001);
			result.put("body", workbuses);
		}
		return Utility.gson.toJson(result);
	}
	@RequestMapping(value="getValidate1",method=RequestMethod.GET,produces={"application/json;charset=utf-8"})
	@ResponseBody
	public String getValidate1(){
	//	List<Workbuses> workbuses=workBusesService.getValidate1();
		Date startDate=new Date(new Date().getTime()-1000*60*60*5);
		Date endDate=new Date(new Date().getTime()+1000*60*60*24);
		List<Map<String, Object>> workbuses=workBusesService.getInfoByDateAndOrientation("基地", startDate, endDate);
		Map<String, Object> result=new HashMap<>();
		if (workbuses.isEmpty()) {
			result.put("status", 1002);
		}
		else {
			result.put("status", 1001);
			result.put("body", workbuses);
		}
		return Utility.gson.toJson(result);
	}
	@RequestMapping(value="getValidate",method=RequestMethod.GET,produces={"application/json;charset=utf-8"})
	@ResponseBody
	public String getValidate(){		
		Date startDate=new Date();
		Date endDate=new Date(startDate.getTime()+1000*60*60*24*15);
		List<Map<String, Object>> workbuses=workBusesService.getInfoByDate(startDate, endDate);
		Map<String, Object> result=new HashMap<>();
		if (workbuses.isEmpty()) {
			result.put("status", 1002);
		}
		else {
			result.put("status", 1001);
			result.put("body", workbuses);
		}
		return Utility.gson.toJson(result);
	}
	@RequestMapping(value="getInValidate",method=RequestMethod.GET,produces={"application/json;charset=utf-8"})
	@ResponseBody
	public String getInValidate(){
		
		List<Workbuses> workbuses=workBusesService.getInValidate();
		Map<String, Object> result=new HashMap<>();
		if (workbuses.isEmpty()) {
			result.put("status", 1002);
		}
		else {
			result.put("status", 1001);
			result.put("body", workbuses);
		}
		return Utility.gson.toJson(result);
	}
	@RequestMapping(value="getByOrientationAndDate",method=RequestMethod.POST,produces={"application/json;charset=utf-8"})
	@ResponseBody
	public String getByOrientationAndDate(@RequestBody Map<String, String> args){
		String date=args.get("date");
		return null;
	}
	
}
